﻿/////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Eric Bowen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 
// associated documentation files (the "Software"), to deal in the Software without restriction, including 
// without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 
// copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the 
// following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial 
// portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN 
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Author's blog: http://blog.scrappydog.com
// Updated source code available:  http://scrappydb.codeplex.com
/////////////////////////////////////////////////////////////////////////////////////////////////////

using System;
using System.Linq.Expressions;
using System.Reflection;
using NLog;
using Remotion.Linq.Parsing.Structure;
using ScrappyDB.Linq.SdbQueryGeneration;
using ScrappyDB.BaseClasses;

namespace ScrappyDB.Linq
{
    public partial class SdbContext
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        public SdbContext()
        {
            //initialize all child SdbSet properties
            var properties = this.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                p.SetValue(this, Activator.CreateInstance(p.PropertyType), null);
            }
        }

        public string ToSql<T>(Expression queryExpression) where T : class, new()
        {
            var queryModel = QueryParser.CreateDefault().GetParsedQuery(queryExpression);
            var x = new HqlGeneratorQueryModelVisitor<T>();

            return HqlGeneratorQueryModelVisitor<T>.GenerateHqlQuery(queryModel).CreateQuery();
        }

        /// <summary>
        /// Saves pending Adds/Updates/Deletes to all entities in the context (includes are SdbSet property collections...)
        /// </summary>
        public void SaveChanges()
        {
            var properties = this.GetType().GetProperties();
            foreach (PropertyInfo p in properties)
            {
                if (p.PropertyType.Name == "SdbSet`1")
                {
                    MethodInfo m = p.PropertyType.GetMethod("SaveChanges");
                    m.Invoke(p.GetValue(this, null),null);
                }
            }
        }

        public SdbEntityEntry<T> Entry<T>(T entity) where T : class, new()
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            //TODO:
            //throw new NotImplementedException();
        }
    }
}